package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.view.ContextThemeWrapper;
import android.support.v7.widget.PopupMenu;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.activity.JYCoordingActivity.AddCoorCustomerActivity;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class CoorDetailedCustomerActivity extends BaseActivity {
    TextView txt_name,txt_manager,txt_age,txt_gender,txt_IDNumber,txt_medical_category,txt_security_numbers,
            txt_xzqy,txt_home_address,txt_manager1,txt_manager2,txt_department,txt_creater,txt_rhfk,txt_hydj,
            txt_create_date,txt_updater,txt_next_content,txt_next_time,txt_bz,txt_main_contact,txt_respectfully_called,txt_phone,txt_email,txt_weibo,txt_zip_code,txt_address;
    private CustomerMessageEntity customerMessageEntity;
    ContactMessageEntity contact;
    private PopupMenu popupMenu;
    private List<CustomerMessageEntity> list;
    private Menu menu;
    SharedPreferences sp;
    JSONObject json1;
    CustomerMessageEntity customer;
    private ImageView img_find,img_luru;
    private ImageView img_update,img_delete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coor_detailed_customer);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        img_find= (ImageView) findViewById(R.id.item_image_cx);
        img_luru= (ImageView) findViewById(R.id.item_image_lr);
        txt_name= (TextView)findViewById(R.id.fragment_customer_base_customerName);
        img_delete= (ImageView) findViewById(R.id.detailed_doc_img_delete1);
        img_update= (ImageView) findViewById(R.id.detailed_doc_img_update);
        txt_manager= (TextView) findViewById(R.id.fragment_customer_base_manager);
        txt_age= (TextView) findViewById(R.id.fragment_customer_base_customerAge);
        txt_gender= (TextView) findViewById(R.id.fragment_customer_base_customergender);
        txt_IDNumber= (TextView) findViewById(R.id.fragment_customer_base_customer_IDNumber);
        txt_medical_category= (TextView)findViewById(R.id.fragment_customer_base_medical_category);
        txt_security_numbers= (TextView) findViewById(R.id.fragment_customer_base_security_numbers);
        txt_xzqy= (TextView) findViewById(R.id.fragment_customer_base_xzqy);
        txt_home_address= (TextView) findViewById(R.id.fragment_customer_base_home_address);
        txt_manager1= (TextView) findViewById(R.id.frm_customer_detailed_base_txt_manager);
        txt_manager2= (TextView) findViewById(R.id.frm_customer_line);
        txt_rhfk = (TextView) findViewById(R.id.fragment_customer_other_base_rhfk);
        txt_hydj = (TextView) findViewById(R.id.fragment_customer_other_base_hydj);
        txt_department = (TextView) findViewById(R.id.fragment_customer_other_base_department);
        txt_creater = (TextView) findViewById(R.id.fragment_customer_other_base_create_people);
        txt_create_date = (TextView) findViewById(R.id.fragment_customer_other_base_create_date);
        txt_updater = (TextView) findViewById(R.id.fragment_customer_other_base_update_people);
        txt_next_content = (TextView) findViewById(R.id.fragment_customer_other_base_next_content);
        txt_next_time = (TextView) findViewById(R.id.fragment_customer_other_base_next_time);
        txt_bz = (TextView) findViewById(R.id.fragment_customer_other_base_remark);
        txt_main_contact= (TextView) findViewById(R.id.fragment_detailed_other_txt_main_contact);
        txt_respectfully_called= (TextView) findViewById(R.id.fragment_detailed_other_txt_respectfully_called);
        txt_phone= (TextView) findViewById(R.id.fragment_detailed_other_txt_phone);
        txt_email= (TextView) findViewById(R.id.fragment_detailed_other_txt_email);
        txt_weibo= (TextView) findViewById(R.id.fragment_detailed_other_txt_weibo);
        txt_zip_code= (TextView) findViewById(R.id.fragment_detailed_other_txt_zip_code);
        txt_address= (TextView)findViewById(R.id.fragment_detailed_other_txt_address);
        customer = GetCustumer.get();
        contact = GetContact.get();
        initData();
        img_luru.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(CoorDetailedCustomerActivity.this, JDcoorActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(CoorDetailedCustomerActivity.this, JDcoorActivity.class);
                intent.putExtra("ii","ii");
                startActivity(intent);
                finish();
            }
        });
        img_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder;
                builder = new AlertDialog.Builder(new ContextThemeWrapper(CoorDetailedCustomerActivity.this, R.style.custom_dialog));
                builder.setTitle("您确定要删除吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                SC sc = new SC();
                                JSONObject object = sc.storage(sp);
                                try {
                                    object.put("KAA01", customer.getClient_id());
                                    String json = object.toString();
                                    String result = NetWorkUtils.getCount(baseClass.delete_coor_customer_URL, json);
                                    json1 = new JSONObject(result);
                                    String boolen = json1.getString("ifsuccess");
                                    if ("true".equals(boolen)) {
                                        finish();
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        if (json1.has("flag")) {

                                            try {
                                                int a = json1.getInt("flag");

                                                if (a == 0) {
                                                    boolean b = json1.getBoolean("ifsuccess");
                                                    if (b) {
                                                        finish();
                                                    } else {
                                                        Toast.makeText(CoorDetailedCustomerActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                                                    }
                                                } else {
                                                    Toast.makeText(CoorDetailedCustomerActivity.this, "该客户有预约信息，不允许删除", Toast.LENGTH_SHORT).show();
                                                }
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }

                                        } else {
                                            Toast.makeText(CoorDetailedCustomerActivity.this, "连接数据失败", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        }).start();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.show();
            }
        });
        img_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CoorDetailedCustomerActivity.this, AddCoorCustomerActivity.class);
                intent.putExtra("tag","update");
                startActivity(intent);
                finish();
            }
        });
    }

    private void initData() {
        txt_name.setText(customer.getClient_name());
        txt_manager.setText(customer.getClient_manager_name());
        txt_age.setText(String.valueOf(customer.getClient_age()));
        if (customer.getClient_gender()!=0){
            if (customer.getClient_gender()==1){
                txt_gender.setText("男");
            }else if (customer.getClient_gender()==2){
                txt_gender.setText("女");
            }
        }else{
            txt_gender.setText("性别未填写");
        }
        txt_IDNumber.setText(customer.getIDNumber());
        if (1==customer.getRhfk()){
            txt_rhfk.setText("未表态");
        }
        if (2==customer.getRhfk()){
            txt_rhfk.setText("糊涂");
        }
        if (3==customer.getRhfk()){
            txt_rhfk.setText("语言沟通障碍");
        }
        if (4==customer.getRhfk()){
            txt_rhfk.setText("未见面");
        }
        if (5==customer.getRhfk()){
            txt_rhfk.setText("无自主活动能力");
        }
        if (6==customer.getRhfk()){
            txt_rhfk.setText("拒绝沟通");
        }
        if (7==customer.getRhfk()){
            txt_rhfk.setText("不能说积分");
        }
        if (8==customer.getRhfk()){
            txt_rhfk.setText("有介绍意向");
        }
        if (9==customer.getRhfk()){
            txt_rhfk.setText("介绍意向强");
        }
        if (10==customer.getRhfk()){
            txt_rhfk.setText("家属有介绍意向");
        }
        if (11==customer.getRhfk()){
            txt_rhfk.setText("准优质会员");
        }
        if (12==customer.getRhfk()){
            txt_rhfk.setText("亲友近视眼");
        }
        if (13==customer.getRhfk()){
            txt_rhfk.setText("无意向");
        }
        if (14==customer.getRhfk()){
            txt_rhfk.setText("征兵入伍");
        }
        if (15==customer.getRhfk()){
            txt_rhfk.setText("其他");
        }
        if (1==customer.getHydj()){
            txt_hydj.setText("A");
        }
        if (2==customer.getHydj()){
            txt_hydj.setText("B");
        }
        if (3==customer.getHydj()){
            txt_hydj.setText("C");
        }
        if (4==customer.getHydj()){
            txt_hydj.setText("VIP");
        }
        if (5==customer.getHydj()){
            txt_hydj.setText("回收组");
        }
        txt_department.setText(customer.getBelongs_department());
        txt_next_time.setText(customer.getNext_Time());
        txt_create_date.setText(customer.getCreate_time());
        txt_next_content.setText(customer.getNext_Content());
        txt_creater.setText(customer.getCurrent_create_user_name());
        txt_updater.setText(customer.getCurrent_update_user_name());
        txt_bz.setText(customer.getAdditional_remarks());
        txt_main_contact.setText(contact.getContact_name());
        txt_respectfully_called.setText(contact.getContact_respectful());
        txt_phone.setText(contact.getFirst_contact_mobile_phone());
        txt_email.setText(contact.getContact_wechat());
        txt_zip_code.setText(contact.getContact_zip_code());
        txt_weibo.setText(contact.getContact_qq());
        txt_address.setText(contact.getContact_address());
        txt_security_numbers.setText(customer.getSecurity_numbers());
        if (customer.getPname()!=null) {
            txt_xzqy.setText(customer.getPname() + customer.getCname() + customer.getAname());
        }
        txt_home_address.setText(customer.getClient_Address());
        txt_medical_category.setText(customer.getMedical_class());
    }

    public void OnClick(View view) {
        switch (view.getId()){
            case R.id.health_education_img_back:
                finish();
                break;
            case R.id.detailed_dock_img_more:
                popupMenu.show();


        }
    }
}
